package cn.dzhang.rcp.hyperbola.editors;

import java.util.concurrent.TimeUnit;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.EditorPart;

import cn.dzhang.rcp.hyperbola.common.UIConstants;

public class ChatEditor extends EditorPart {

	public static final String ID = "cn.dzhang.rcp.hyperbola.editors.ChatEditor"; //$NON-NLS-1$
	private Text txtTranscript;
	private Text txtEntry;
	private Composite cmpst;
	private boolean dirty;

	public ChatEditor() {
	}

	/**
	 * Create contents of the editor part.
	 * 
	 * @param parent
	 */
	@Override
	public void createPartControl(Composite parent) {

		SashForm sashForm = new SashForm(parent, SWT.VERTICAL);

		txtTranscript = new Text(sashForm, SWT.BORDER | SWT.READ_ONLY
				| SWT.WRAP | SWT.MULTI);

		Composite composite = new Composite(sashForm, SWT.NONE);
		composite.setLayout(new GridLayout(1, false));

		txtEntry = new Text(composite, SWT.BORDER | SWT.WRAP | SWT.MULTI);
		txtEntry.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1,
				1));

		cmpst = new Composite(composite, SWT.NONE);
		cmpst.setLayout(new GridLayout(1, false));
		cmpst.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1));

		Button btnSend = new Button(cmpst, SWT.NONE);
		btnSend.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				sendMessage();
			}
		});
		btnSend.setImage(UIConstants.Images.EMAIL_SEND_RECEIVE_16);
		GridData gd_btnSend = new GridData(SWT.LEFT, SWT.CENTER, false, false,
				1, 1);
		gd_btnSend.widthHint = 100;
		btnSend.setLayoutData(gd_btnSend);
		btnSend.setText("Send");
		sashForm.setWeights(new int[] { 7, 3 });

	}

	protected void sendMessage() {
		String message = txtEntry.getText().trim();

		if (!message.isEmpty()) {
			txtTranscript.append(renderMessage(getUser(), message));
			txtTranscript.append("\n");

			scrollToEnd();
			txtEntry.setText("");
			this.dirty = true;
			firePropertyChange(PROP_DIRTY);
		}
	}

	private void scrollToEnd() {
		int n = txtTranscript.getCharCount();
		txtTranscript.setSelection(n, n);
		txtTranscript.showSelection();
	}

	private String renderMessage(String user, String message) {
		return String.format("<%s>  %s", user, message);
	}

	@Override
	public void setFocus() {
		txtEntry.setFocus();
	}

	@Override
	public void doSave(IProgressMonitor monitor) {
		monitor.beginTask("保存通讯记录", IProgressMonitor.UNKNOWN);
		try {
			TimeUnit.SECONDS.sleep(1);
		} catch (Exception e) {
		}
		dirty = false;
		firePropertyChange(PROP_DIRTY);
	}

	@Override
	public void doSaveAs() {
	}

	@Override
	public void init(IEditorSite site, IEditorInput input)
			throws PartInitException {
		setSite(site);
		setInput(input);
		setPartName(getUser());
	}

	private String getUser() {
		return getChatEditorInput().getName();
	}

	private ChatEditorInput getChatEditorInput() {
		return (ChatEditorInput) getEditorInput();
	}

	@Override
	public boolean isDirty() {
		return dirty;
	}

	@Override
	public boolean isSaveAsAllowed() {
		return true;
	}
}
